// import {
// 	getUserInfo
// } from "@/api/user.js";
import {
	LOGIN_STATUS,
	USER_INFO,
	UID,
	MY_CITY
} from '@/config/cache';
import Cache from '@/utils/cache'; 
 
// console.log(Cache.get(MY_CITY))
const state = {
	token: Cache.get(LOGIN_STATUS) || false, 
	userInfo: JSON.parse(Cache.get(USER_INFO) || '{}'),
	uid: Cache.get(UID) || null,
	city: JSON.parse(Cache.get(MY_CITY) || "{}")
};

const mutations = {
	//一键、手机号、密码登录处理
	PUBLICLOGIN(state, res){
		if( res.token ){
			this.commit('LOGIN', {
				token: res.token 
			});
		};
		this.commit('UPDATE_USERINFO', {
			phone: res.phone?res.phone:"",
			userName: res.userName?res.userName:""
		});
	},
	LOGIN(state, opt) {
		state.token = opt.token;
		Cache.set(LOGIN_STATUS, opt.token, opt.time);
	},
	SETUID(state,val){
		state.uid = val;
		Cache.set(UID, val);
	},
	UPDATE_LOGIN(state, token) {
		state.token = token;
	},
	LOGOUT(state) {
		state.token = undefined;
		state.uid = undefined  
		Cache.clear(LOGIN_STATUS); 
		Cache.clear(UID); 
		Cache.clear(USER_INFO); 
	}, 
	UPDATE_USERINFO(state, userInfo) {
		state.userInfo = userInfo;
		Cache.set(USER_INFO, userInfo);
	},
	//选择城市
	SELECT_CITY(state, item) {
		state.city = item;
		Cache.set(MY_CITY, item);
	}
};

const actions = {
	// USERINFO({
	// 	state,
	// 	commit
	// }, force) {
	// 	if (state.userInfo !== null && !force)
	// 		return Promise.resolve(state.userInfo);
	// 	else
	// 		return new Promise(reslove => {
	// 			getUserInfo().then(res => {
	// 				commit("UPDATE_USERINFO", res.data);
	// 				Cache.set(USER_INFO, res.data);
	// 				reslove(res.data);
	// 			});
	// 		}).catch(() => {

	// 		});
	// }
};

export default {
	state,
	mutations,
	actions
};
